package com.aoyy.action.user;

import java.sql.Timestamp;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ModelDriven;
import com.aoyy.action.BaseAction;
import com.aoyy.model.User;
import com.aoyy.util.StringFormatter;
/**============================================================================================
 * <code>UserAction</code> action for <code>User</code> including login, logout and register.
 * @author Administrator
 * @date 2017-07-13
 * ============================================================================================= */
@Scope("prototype")
@Controller("userAction")
public class UserAction extends BaseAction implements ModelDriven<User> {
  private static final long serialVersionUID = 379569166520486476L;
  private User user = new User();
  private String confirmPassword;
  
  public void setUser(User user) {
	this.user = user;
  }
  public User getUser() {
	return user;  
  }
  
  public void setConfirmPassword(String confirmPassword) {
	this.confirmPassword = confirmPassword;  
  }
  public String getConfirmPassword() {
	return confirmPassword;  
  }
    
  @Override
  public User getModel() {
	return user;
  }
  
  public String toLogin() {
	return LOGIN;  
  }
  
  public String login() {
    if (user.getUserName() == null || user.getUserName().isEmpty()) {
	  addFieldError("userName", "User Name is required.");
	  return LOGIN;
    }
	if (user.getPassword() == null || user.getPassword().isEmpty()) {
	  addFieldError("password", "Password is required.");
	  return LOGIN;
	}
    User user = userDao.login(this.user);
    if (user == null) {
      addFieldError("", "Incorrect UserName or Password.");
      return LOGIN;
    }
	session.put("user", user);
	return INDEX;
  }
  
  public String logout() {
	if (session.get("user") != null)  {
      session.put("user", null);		
	}
	return INDEX;
  }
  
  public String toRegister() {
	return REGISTER;  
  }
  
  public String register() {
	if (user.getUserName() == null || user.getUserName().isEmpty()) {
      addFieldError("userName", "User Name is required.");	
      return REGISTER;	
    } else if (user.getUserName().length() > 45) {
	  addFieldError("userName", "User Name is required and to be no more than 45 characters.");
	  return REGISTER;
    }
	if (user.getPassword() == null || user.getPassword().isEmpty()) {
	  addFieldError("password", "Password is required.");
	  return REGISTER;
    } else if (user.getPassword().length() < 6 || user.getPassword().length() > 12) {
	  addFieldError("password", "Password is required to be between 6 and 12 characters.");
	  return REGISTER;
    }
	if (confirmPassword == null || confirmPassword.isEmpty()) {
	  addFieldError("", "Confirm Password is required.");
	  return REGISTER;
	} else if (!user.getPassword().equals(confirmPassword)) {
	  addFieldError("", "Confirm Password is inconsistent with Password.");
	  return REGISTER;
    }
	if (user.getBirthday() == null || user.getBirthday().isEmpty()) {
	  addFieldError("birthday", "Birthday is required.");
	  return REGISTER;
    }  
	if (!userDao.isUniqueUser(user)) {
	  addFieldError("", "The user using this name has been registered.");
	  return REGISTER;
	}
	user.setCreatedTime(Timestamp.valueOf(StringFormatter.getDateTimeStr()));
	user.setPermission(0);
	userDao.save(user);
	return INDEX;
  }
 
}
